﻿using System.Collections.Generic;

namespace PGMapper
{
    // this exists only to help us access the changetracker through reflection.emit a bit easier.
    public interface ITrackChanges
    {
        ChangeTracker Tracker { get; }
    }

    /// <summary>
    /// Helper class to keep track of property changes without baking it into every emitted subclass.
    /// </summary>
    public class ChangeTracker
    {
        Dictionary<string, object> changedProperties = new Dictionary<string, object>();

        public bool ExistsInDatabase { get; set; }
        public PostgresDatabase DB { get; set; }

        public void PropertyChanged(string propertyName)
        {
            // we don't care what the value is, we just want to store the keys in an efficient way
            changedProperties[propertyName] = null;
        }

        public IEnumerable<string> ChangedProperties
        {
            get { return changedProperties.Keys; }
        }

        public int ChangedPropertyCount
        {
            get { return changedProperties.Count; }
        }

        public void ClearChanges()
        {
            changedProperties.Clear();
        }
    }
}
